package cn.scau.server.service.impl;

import cn.scau.server.pojo.Notification;
import cn.scau.server.mapper.NotificationMapper;
import cn.scau.server.service.NotificationService;
import cn.scau.server.vo.ResultVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 代码不跑我先跑
 * @since 2025-03-30
 */
@Service
public class NotificationServiceImpl extends ServiceImpl<NotificationMapper, Notification> implements NotificationService {
    @Autowired
    private NotificationMapper notificationMapper;

    // 根据消息id查找消息
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultVO<Notification> getMessageById(Integer id){
        // 根据消息id查找消息
        QueryWrapper<Notification> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        Notification notification = notificationMapper.selectOne(queryWrapper);
        if (notification == null) {
            return ResultVO.fail("消息不存在");
        }
        return ResultVO.success("查找成功", notification);
    }

    //修改未读为已读
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultVO changeIsReadById(Integer id){
        // 根据消息id查找消息
        Notification updateEntity = new Notification();
        updateEntity.setId(id);
        updateEntity.setIsRead(1);

        // 执行更新（返回受影响行数）
        int success = notificationMapper.updateById(updateEntity);
        if (success > 0) {
            return ResultVO.success("消息已标记为已读");
        } else {
            return ResultVO.fail("消息不存在");
        }
    }

    //根据员工编号查找所有消息
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultVO<List<Notification>> getMessageByEmpId(String id){
        if (id == null) {
            return ResultVO.fail("员工编号不能为空");
        }

        QueryWrapper<Notification> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("emp_id", id);
        List<Notification> notifications = notificationMapper.selectList(queryWrapper);

        if (notifications == null || notifications.isEmpty()) {
            return ResultVO.success("暂无消息");
        }
        return ResultVO.success("查找成功", notifications);
    }

    // 判断员工是否有未读消息
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultVO checkUnreadMessages(String id){
        if (id == null) {
            return ResultVO.fail("员工编号不能为空");
        }
        QueryWrapper<Notification> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("emp_id", id);
        List<Notification> notifications = notificationMapper.selectList(queryWrapper);

        if (notifications == null || notifications.isEmpty()) {
            return ResultVO.success("暂无消息");
        }

        boolean allRead = true;
        for (Notification notification : notifications) {
            if (notification.getIsRead() == 0) {
                allRead = false;
                break;
            }
        }

        if (allRead) {
            return ResultVO.success("没有未读消息",1);
        } else {
            return ResultVO.success("有未读消息",0);
        }
    }

}
